package guoanmaker.operator.business.repository;


import java.util.Date;
import java.util.List;

import javax.persistence.LockModeType;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import guoanmaker.operator.business.model.OperatorLuckdrawRecord;

public interface OperatorLuckdrawRecordRepository extends JpaRepository<OperatorLuckdrawRecord, String>, JpaSpecificationExecutor<OperatorLuckdrawRecord> {
	@Query("select OLR from OperatorLuckdrawRecord OLR where OLR.activityId=:activityId")
	public List<OperatorLuckdrawRecord> queryRecordByActivityId(@Param("activityId")String activityId);
	@Query("select OLR from OperatorLuckdrawRecord OLR where OLR.userId=:userId AND OLR.activityId=:activityId")
	public List<OperatorLuckdrawRecord> queryRecordByUserId(@Param("userId")String userId,@Param("activityId")String activityId);
	@Query("select OLR from OperatorLuckdrawRecord OLR where OLR.recordTime<=:maxTime ")
	public List<OperatorLuckdrawRecord>	queryRecordByMaxTime(@Param("maxTime")Date maxTime);
	@Query("select OLR from OperatorLuckdrawRecord OLR where OLR.recordTime>=:minTime ")
	public List<OperatorLuckdrawRecord>	queryRecordByMinTime(@Param("minTime")Date minTime);
	@Query("select OLR from OperatorLuckdrawRecord OLR where OLR.recordTime>=:minTime AND OLR.recordTime<=:maxTime")
	public List<OperatorLuckdrawRecord>	queryRecordByTime(@Param("minTime")Date minTime,@Param("maxTime")Date maxTime);
	@Lock(LockModeType.PESSIMISTIC_READ) 
	@Query("select COUNT(OLR.id) from OperatorLuckdrawRecord OLR where OLR.userId=:userId AND OLR.activityId=:activityId")
	public int queryCountByUseridAndActivityId(@Param("userId")String userId,@Param("activityId")String activityId)throws Exception; 
	@Lock(LockModeType.PESSIMISTIC_READ) 
	@Query("select COUNT(OLR.id) from OperatorLuckdrawRecord OLR where OLR.userId=:userId AND OLR.activityId=:activityId AND OLR.recordTime<:timeTwo and OLR.recordTime>=:timeOne ")
	public int queryCountByUseridAndActivityIdAndTime(@Param("userId")String userId,@Param("activityId")String activityId,@Param("timeOne")Date timeOne,@Param("timeTwo")Date tirmeTwo)throws Exception;
	@Query("SELECT  COUNT(DISTINCT a.userId)   FROM OperatorLuckdrawRecord a where a.activityId=:activityId")
	public Integer selectByActivityId(@Param("activityId") String activityId);
	@Query("select COUNT(OLR.id) from OperatorLuckdrawRecord OLR where OLR.activityId=:activityId")
	public Integer queryRecordCount(@Param("activityId") String activityId);
	
	
}